package com.hanbang.gycatering.comeon;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;
import com.hanbang.gangyu.R;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.bean.UserInforData;
import com.hanbang.gycatering.classify.ClassifyActivity;
import com.hanbang.gycatering.guide.GuideActivity;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.homepage.HomePageActivity;
import com.hanbang.gycatering.utils.DbUtil;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyLog;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MySharedPreferences;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class WelcomeActivity extends BaseActivity {
	private int time = 2000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		checkGouWuCheNumber();
		ImageView view = new ImageView(this);
		view.setBackgroundResource(R.drawable.welcome);
		view.setScaleType(ScaleType.FIT_XY);
		setContentView(view);
		// postService(Constant.GETPOINT);

		final Timer timer = new Timer(true);
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				if (isLogin(false)) {
					postService();
				}
				Intent intent = new Intent(WelcomeActivity.this,
						HomePageActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				finish();
			}
		}, time);

	}

	@Override
	public void onBackPressed() {
	};

	Handler handler = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if (msg.what == 110) {
				Intent intent = new Intent(WelcomeActivity.this,
						GuideActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				finish();
			}
			if (msg.what == 111) {
				Intent intent = new Intent(WelcomeActivity.this,
						HomePageActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				finish();
			}
			if (msg.what == 112) {
				Intent intent = new Intent(WelcomeActivity.this,
						ClassifyActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				finish();
			}

		}
	};

	// 手机号登入
	private void postService() {
		MyLog.e("ahahahaha1",
				HttpInterfaces.loginuser + "&userphone="
						+ userData.getUserphone() + "&password="
						+ userData.getUserpassword());
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET,
				HttpInterfaces.loginuser + "&userphone="
						+ userData.getUserphone() + "&password="
						+ userData.getUserpassword(),
				new MyRequestCallBack<String>(WelcomeActivity.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						UserInforData data = null;
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int state = json.getInt("state");
							if (state == 1) {
								data = new UserInforData();
								JSONObject userinfo = json.getJSONArray("info")
										.getJSONObject(0);
								data.setUserId(userinfo.getInt("id"));
								data.setBalanc(userinfo.getString("balance"));
								data.setUsernick(userinfo.getString("name"));
								data.setUserphone(userinfo.getString("phone"));
								data.setUserpassword(userinfo
										.getString("password"));
								data.setPiont(userinfo.getString("point"));
								data.setAvatar(userinfo.getString("head"));
								data.setAddress(userinfo.getString("address"));
								data.setGrade(userinfo.getString("grade"));
								data.setPlotid(userinfo.getInt("plotid"));
								data.setPlotname(userinfo.getString("plotname")); 
								data.setRealname(userinfo.getString("realname"));
								data.setProvince(userinfo.getString("address"));
								if (data != null) {
									DbUtils dbUtils = DbUtil
											.getDbUtils(WelcomeActivity.this);
									// 把id 保存在sp
									MySharedPreferences
											.setSharedPreferencesKeyAndValue(
													WelcomeActivity.this,
													MySharedPreferences.USER_DATA,
													"user_id", data.getUserId());
									try {
										UserInforData u = dbUtils.findById(
												UserInforData.class,
												data.getUserId());
										if (u == null) {
											dbUtils.save(data);
										} else {
											dbUtils.saveOrUpdate(data);
										}
										MyLog.e("dbUtils", "保存成功");
									} catch (DbException e) {
										e.printStackTrace();
										MySharedPreferences
												.setSharedPreferencesKeyAndValue(
														WelcomeActivity.this,
														MySharedPreferences.USER_DATA,
														"user_id", -1);
									}
									finish();
								}
							} else {
								MySharedPreferences
										.setSharedPreferencesKeyAndValue(
												WelcomeActivity.this,
												MySharedPreferences.USER_DATA,
												"user_id", -1);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	/*
	 * 检查购物车数量
	 */
	private void checkGouWuCheNumber() {
		if (isLogin(false)) {
			((MyApplication) getApplicationContext()).httpUtils.send(
					HttpMethod.GET, HttpInterfaces.carsum + "&password="
							+ userData.getUserpassword() + "&userphone="
							+ userData.getUserphone(),
					new MyRequestCallBack<String>(WelcomeActivity.this, null) {
						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							super.onSuccess(responseInfo);
							try {
								JSONObject json = new JSONObject(
										responseInfo.result);
								int state = json.getInt("state");
								if (state == 1) {
									int info = json.getInt("info");
									MySharedPreferences
											.setSharedPreferencesKeyAndValue(
													WelcomeActivity.this,
													MySharedPreferences.USER_DATA,
													"gouwucheNumber", info);

								}
							} catch (JSONException e) {
								e.printStackTrace();
							}

						}
					});
		}
	}
}
